angular.
module('userList').
component('userList', {
  templateUrl:
  'views/app/user-list/user-list.template.html',
  controller: function ($scope,$cookies,$http,$window,$location,$rootScope,convert) {
        // $scope.myRights = readCookie('rights')
        // if ($scope.myRights != undefined){
        //   if ($scope.myRights.indexOf('user') == -1){
        //               swal("没有操作权限", '请联系管理员!', "error").then(function(){
        //     })
        //   return false
        // }
        
        // }
        // else{
        //   $window.location = '/login'
        // }

        $scope.genderSelect = -1
        $scope.keyword = ''
        $scope.genders = [{name:'全部',value:-1},{name:'男',value:0},{name:'女',value:1}]
        var condition = {}




//         $scope.export = function(){

//             // reuest, get res, then 
// $scope.myRights = readCookie('rights')
//         if ($scope.myRights != undefined){
//           if ($scope.myRights.indexOf('user') == -1){
//                       swal("没有操作权限", '请联系管理员!', "error").then(function(){
//             })
//           return false
//         }
        
//         }
//         else{
//           $window.location = '/login'
//         }
//             $http({
//               method: 'post',
//               url: '/admin/userExport',
//               data : {'session':$cookies.get('sessionID')}
              
//           }).success(function(datax) {
//               if (datax.code != 200){
//                   swal("出错了", '', "error").then(function(){
                    
//                   })
//                   return false                
//               }
//               else{
//                 var blob = new Blob([convert.base64ToArrayBuffer(datax.data)]);
//                 saveAs(new Blob([convert.base64ToArrayBuffer(datax.data)],{type:""}), "用户列表.xlsx",function(){
//                   $scope.reset()
//                 });

//               }
//           })


//         }

        function getUserData(query,callback){
          query.session = $cookies.get('sessionID')
          $http({
              method: 'post',
              url: '/admin/exportMyUsers',
              data : query
          }).success(function(datax) {
            if (datax.code != 200){
                      swal("出错了", '', "error").then(function(){
                        
                      })
                      return false                
                  }
                  else{
                    var blob = new Blob([convert.base64ToArrayBuffer(datax.data)]);
                    saveAs(new Blob([convert.base64ToArrayBuffer(datax.data)],{type:""}), "“" + $scope.keyword + "”" + "粉丝列表.xlsx",function(e){
                      console.log(e,'e')

                    });

                    // var blob = new Blob([datax.data], {type: 'text/plain'});
                    // saveAs(blob, $scope.keyword + '粉丝列表.txt')

            }
            
            //paginationReset(1,1)
            callback()
          })          
        }



      $scope.search = function(){
        condition = {}
        condition.keyword = $scope.keyword
        condition.gender = $scope.genderSelect

        getUserData(condition,function(){
                  
        })        
      }


      $scope.sort = function(index){
        
        var query = ''

       
        condition = {}
        if ($scope.keyword != ''){
          condition.keyword = $scope.keyword
        }
        condition.gender = $scope.genderSelect

        getUserData(condition,function(){        
        })
      }
      function formatDate(date){
        var xdate = new Date(date)
        var year = xdate.getFullYear()
        var day = xdate.getDate()
        var month = xdate.getMonth()
        var hour = xdate.getHours()
        var minute = xdate.getMinutes()
        return String(year) + '-' + String(month+1) + '-' + String(day) + ' ' + String(addZeroBefore(hour)) + ':' + String(addZeroBefore(minute))
      }

      function formatCellphone(data){
        console.log(data)
        return String(data).substr(0,3) + '****' +String(data).substr(7,10) 
      }


      function addZeroBefore(n) {
        return (n < 10 ? '0' : '') + n;
      }

  }
})



